package pdfsetter.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.pdfbox.pdmodel.PDDocument;

import pdfsetter.core.pages.PageInfo;
import pdfsetter.core.pages.PageRectangle;
import pdfsetter.core.pages.PageType;
import pdfsetter.core.schema.AutomaticDocumentSchemaPattern;
import pdfsetter.core.schema.DocumentSchemaPattern;
import pdfsetter.core.schema.SinglePageSchema;
import pdfsetter.core.schema.onefile.OneFileDocumentSchemaPattern;
import pdfsetter.core.schema.onefile.SimpleOneFileDocumentSchemaPattern;
import pdfsetter.core.schema.selectors.RangePageSelector;

import com.thoughtworks.xstream.XStreamException;

public class Setting4On1Example {
    private static void printPageSchema(SinglePageSchema ps) {
        System.out.println("PageSchema:\t" + ps.getDescription());
        System.out.println(String.format("Page (width x height) -> (%f,%f)", ps.getPageInfo().getWidth(), ps.getPageInfo().getHeight()));
        for (PageRectangle pr : ps.getPageLocations()) {
            System.out.println(String.format("Rectangle: (hor,ver) shift, width x height, rotation -> (%f,%f) | (%f x %f) | %f", pr.getHorizontalShift(),
                    pr.getVerticalShift(), pr.getWidth(), pr.getHeight(), pr.getRotation()));
        }
    }

    public static void main(String[] args) throws XStreamException, FileNotFoundException {
        PageInfo a4 = new PageInfo(PageType.A4);
        ArrayList<PageRectangle> loc = new ArrayList<PageRectangle>();
        loc.add(new PageRectangle(0, 0, a4.getWidth() / 2, a4.getHeight() / 2, 0));
        loc.add(new PageRectangle(a4.getWidth() / 2, 0, a4.getWidth() / 2, a4.getHeight() / 2, 0));
        loc.add(new PageRectangle(0, a4.getHeight() / 2, a4.getWidth() / 2, a4.getHeight() / 2, 0));
        loc.add(new PageRectangle(a4.getWidth() / 2, a4.getHeight() / 2, a4.getWidth() / 2, a4.getHeight() / 2, 0));
        SinglePageSchema fourOnA4 = new SinglePageSchema("4xA6 on A4", a4, loc);
        printPageSchema(fourOnA4);

        ArrayList<PageRectangle> loc0 = new ArrayList<PageRectangle>();
        loc0.add(new PageRectangle(0, 0, a4.getWidth(), a4.getHeight(), 0));
        SinglePageSchema normalA4 = new SinglePageSchema("Normal A4", a4, loc0);
        printPageSchema(normalA4);

        List<OneFileDocumentSchemaPattern> ofDocPat = new LinkedList<OneFileDocumentSchemaPattern>();
        ofDocPat.add(new SimpleOneFileDocumentSchemaPattern(normalA4, new RangePageSelector(0, 0)));
        ofDocPat.add(new SimpleOneFileDocumentSchemaPattern(fourOnA4, new RangePageSelector(1)));
        DocumentSchemaPattern autChapters = new AutomaticDocumentSchemaPattern(ofDocPat);

        // ------------ Input Files --------------
        List<File> files = new LinkedList<File>();
        files.add(new File("src/test/resources/test-pdf-1.pdf"));
        files.add(new File("src/test/resources/test-pdf-2.pdf"));
        files.add(new File("src/test/resources/test-pdf-img.pdf"));

        // Load source documents and retrieve document sizes
        int[] fileSizes = new int[files.size()];
        int i = 0;
        for (File file : files) {
            try {
                PDDocument pdd = PDDocument.loadNonSeq(file, null);
                fileSizes[i] = pdd.getNumberOfPages();
                System.out.println("File info: " + fileSizes[i] + "(pages)");
                pdd.close();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            i++;
        }
        CompoundSettingExample.compile(autChapters.produceDocumentSchema(fileSizes), files, "out-test/auto-4on_1.pdf");
    }
}
